import QtQuick 1.1
import com.nokia.meego 1.0

Dialog {
    id: inputDialog

    property Style platformStyle: QueryDialogStyle {}

    property string titleString
    property string message
    property string textFieldText

    property string acceptButtonText
    property string rejectButtonText

    signal accepted(string name)

    function selectAll(){
        textField.forceActiveFocus()
        textField.selectAll()
    }

    function openKeyboard(){
        textField.forceActiveFocus()
    }

    title:
        Item{
        id: rowTitle
        width: parent.width
        height: titleText.height

        anchors.left: parent.left
        anchors.bottom: parent.bottom
        anchors.bottomMargin: inputDialog.platformStyle.buttonsColumnSpacing

        Text{
            id: titleText

            text: titleString
            width: parent.width

            anchors.verticalCenter: parent.verticalCenter

            font.family: inputDialog.platformStyle.titleFontFamily
            font.pixelSize: inputDialog.platformStyle.titleFontPixelSize
            font.bold:  inputDialog.platformStyle.titleFontBold
            font.capitalization: inputDialog.platformStyle.titleFontCapitalization
            color: inputDialog.platformStyle.titleTextColor

            horizontalAlignment: Text.AlignHCenter
            verticalAlignment:   Text.AlignVCenter
        }
    }

    content:
        Grid{
        id: contentGrid
        rows: 2
        columns: 1

        width: parent.width

        anchors.horizontalCenter: parent.horizontalCenter
        anchors.verticalCenter: parent.verticalCenter

        spacing: inputDialog.platformStyle.buttonsColumnSpacing

        Text{
            id: messageText

            text: message
            width: parent.width
            height: textField.height

            font.family: inputDialog.platformStyle.messageFontFamily
            font.pixelSize: inputDialog.platformStyle.messageFontPixelSize
            color: inputDialog.platformStyle.messageTextColor

            verticalAlignment:  Text.AlignVCenter
        }

        TextField {
            width: parent.width

            id: textField
            text: textFieldText
        }
    }

    buttons:
        //The following code is taken from QueryDialog.qml

        Item {
        id: buttonColFiller
        width: parent.width
        height: childrenRect.height

        anchors.top: parent.top

        Item {id: dummy; anchors.fill:  parent}

        Grid {
            rows: 2
            columns: 1

            id: buttonGrid
            anchors.top: parent.top
            //anchors.topMargin: inputDialog.platformStyle.buttonTopMargin
            spacing: inputDialog.platformStyle.buttonsColumnSpacing

            /*height: (acceptButton.text  == "" ? 0 : acceptButton.height)
                    + (rejectButton.text == "" ? 0 : rejectButton.height)
                    + anchors.buttonTopMargin *2  + spacing
            */
            anchors.horizontalCenter: parent.horizontalCenter

            Button {
                id: acceptButton
                text: acceptButtonText
                onClicked: {
                    accepted(textField.text)
                }
                visible: text != ""
                __dialogButton: true
                platformStyle: ButtonStyle {inverted: true}
            }
            Button {
                id: rejectButton
                text: rejectButtonText
                onClicked: {
                    inputDialog.destroy()
                }
                visible: text != ""
                __dialogButton: true
                platformStyle: ButtonStyle {inverted: true}
            }
        }
    }

    states: [
        State {
            name: "inLandscape"
            when: !appWindow.inPortrait
            PropertyChanges {
                target: buttonGrid
                rows: 1
                columns: 2
                anchors.topMargin: inputDialog.platformStyle.buttonTopMargin/2
                height: (acceptButton.text  == "" ? 0 : acceptButton.height)
                        + anchors.buttonTopMargin  //+ spacing
            }
            PropertyChanges {
                target: contentGrid
                rows: 1
                columns: 2
                height: textField.height
                }
            PropertyChanges {
                target: messageText
                width: messageText.parent.width * 0.25
            }
            PropertyChanges {
                target: textField
                width: textField.parent.width * 0.75
            }
        },
        State {
            name: "inPortrait"
            when: appWindow.inPortrait
            PropertyChanges {
                target: buttonGrid
                rows: 2
                columns: 1
                anchors.topMargin: inputDialog.platformStyle.buttonTopMargin
                height: (acceptButton.text  == "" ? 0 : acceptButton.height)
                        + (rejectButton.text == "" ? 0 : rejectButton.height)
                        + anchors.buttonTopMargin *2 + spacing
            }
            PropertyChanges {
                target: contentGrid
                rows: 2
                columns: 1
                height: textField.height+messageText.height
            }
            /*PropertyChanges {
                target: messageText
                width: messageText.parent.width
                height: textField.height
            }*/
            /*PropertyChanges {
                target: textField
                width: textField.parent.width
            }*/
        }
    ]
}
